package com.qidian.tcba.admin.api.controller;


import com.qidian.tcba.admin.model.dto.user.*;
import com.qidian.tcba.admin.model.param.LoginParam;
import com.qidian.tcba.admin.model.query.UserQueryDTO;
import com.qidian.tcba.admin.service.UserService;
import com.qidian.tcba.utils.JSONResult;
import com.qidian.tcba.utils.Message;
import com.qidian.tcba.utils.result.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 冯永龙
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/user")
@Api(tags ="用户")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("更新密码")
    @PostMapping("/updatePassword")
    public JsonResult updatePassword(@RequestBody UpdateUserPasswordDTO updateUserPassword){
        boolean result = userService.updateUserPassword(updateUserPassword);
        if (result){
            return JsonResult.success();
        }else {
            return JsonResult.error();
        }
    }

    @ApiOperation(notes = "停车用户单位管理",value = "查询全部参数全部传空")
    @PostMapping("/getManageUser")
    public JSONResult<List<UserResultListDTO>> getUserList(@RequestBody UserQueryDTO userQueryDTO){
        List<UserResultListDTO> userResultList = userService.getUserResultList(userQueryDTO);
        return new JSONResult<>(userResultList,200,"成功");
    }

    @ApiOperation("新增用户")
    @PostMapping("/add")
    public JSONResult<Object> addUser(@RequestBody AddUserDTO addUserDTO){
        if (userService.addUser(addUserDTO)) {
            return new JSONResult<>(null,200,"成功");
        }else {
            return new JSONResult<>(null, 201, "失败");
        }
    }

    @ApiOperation("修改用户状态")
    @GetMapping("/updateUserStatus/{userId}/{status}")
    public JSONResult<Object> updateUserStatus(@PathVariable("userId") int userId,@PathVariable("status") int status){
        boolean res = userService.updateUserStatus(userId, status);
        if (res){
            return new JSONResult<>(null,200,"成功");
        }else {
            return new JSONResult<>(null, 201, "失败");
        }
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public JsonResult<UserDTO> login(@RequestBody LoginParam loginParam){
        UserDTO userDTO = userService.login(loginParam.getUsername(), loginParam.getPassword());
        if (userDTO == null){
            return JsonResult.error().data(null);
        }else {
            return JsonResult.success().data("row",userDTO);
        }
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public JsonResult register(@RequestBody RegisterDTO registerDTO){
        boolean res = userService.register(registerDTO);
        if (res){
            return JsonResult.success();
        }else {
            return JsonResult.error();
        }

    }
}

